<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<script type="text/javascript">
var functionActionDataGrid = $('#function-action-datagrid-json').datagrid({
    gridTitle : '功能动作管理',
    showToolbar: true,
    filterThead: false,
    filterAll: true,
    sortAll : true,
    dataUrl: 'function/action/list.do?funSn=${funSn}&systemId=${systemId}',
    dataType: 'json',
    columns: [
        {
            name: 'actionName',
            label: '功能动作名称',
            align: 'center',
            width: 100
        },
        {
            name: 'action',
            label: '动作链接地址',
            align: 'center',
            width: 300
        },
        {
            name: 'status',
            label: '读写类型',
            align: 'center',
            type : "select",
            items: [{'R':'读'}, {'W':'写'}],
            render: $.datagrid.renderItem,
            width: 80
        },
        {
			name: '',
			label: '操作',
			align: 'center',
			width: 160,
			quicksort:false,
			menu: false,
			edit: false,
			render: function(value){
		        var html = '';
		    	html += '<div class="btn-group" role="group"><button type="button" class="btn btn-blue btn-sm" onclick="editFunctionAction(this)"><i class="fa fa-edit"></i> 编辑</button></div>';
			    html += '<div class="btn-group" role="group"><button type="button" class="btn btn-red btn-sm" onclick="delFunctionAction(this)"><i class="fa fa-remove"></i> 删除</button></div>';
			    return html;
			}
        }
    ],
    paging: {pageSize:20, selectPageSize:'20,30,40'},
    showTfoot: true,
    fullGrid: true,
    toolbarCustom: function(){
    	var html = '';
    	html += '<div class="btn-group" role="group"><button type="button" class="btn btn-blue" onclick="addFunctionAction()"><i class="fa fa-plus"></i> 添加</button></div>';
	    html += '<div class="btn-group" role="group"><button type="button" class="btn btn-green" onclick="refreshFunctionActionDataGrid()"><i class="fa fa-refresh"></i> 刷新</button></div>';
	    return html;
    }
});

function addFunctionAction() {
	$('#edit-function-action-dialog').dialog({
		id:'add-function-action-dialog', 
		url:'function/action/add.do?funSn=${funSn}', 
		maxable: false,
		resizable:false,
		mask: true,
		width: 850,
		height: 250,
		onLoad: function($dialog){
			setEditFunctionActionBtnEvent($dialog, true, 'add-function-action-dialog');
		},
		title:'添加功能菜单'
	});
}

function editFunctionAction(_this) {
	var functionActionIndex = $(_this).closest("tr").index();
	var data = functionActionDataGrid.data('bjui.datagrid').getRowData(functionActionIndex);
	$('#edit-function-action-dialog').dialog({
		id:'edit-function-action-dialog', 
		url:'function/action/edit.do?funSn=${funSn}', 
		maxable: false,
		resizable:false,
		mask: true,
		width: 850,
		height: 250,
		onLoad: function($dialog){
			setEditFunctionActionBtnEvent($dialog, true, 'edit-function-action-dialog');
			getJsonData2Form('function/action/getAction.do', '?funSn=${funSn}&funActionSn='+data.funActionSn, $('#function_action_form'), null);
		},
		title:'编辑功能菜单'
	});
}

function delFunctionAction(_this) {
	var functionActionIndex = $(_this).closest("tr").index();
	var rowData = functionActionDataGrid.data('bjui.datagrid').getRowData(functionActionIndex);
	functionActionDataGrid.alertmsg('confirm', '确定删除当前数据？', {
		okName:'确定', 
		cancelName:'取消', 
		title:'提示信息',
		okCall : function(){
			var url = "function/action/del.do?funActionSn="+rowData.funActionSn;
			var data = null;
			var successCallBack = function(result){
				refreshFunctionActionDataGrid();
			}
			var failCallBack = null;
			var errorCallBack = null;
			myAjax(url, data, true, successCallBack, failCallBack, errorCallBack);
       	}
	});
}

function setEditFunctionActionBtnEvent($dialog, isAdd, dialogId) {
	var $save, $cancel;
	$save = $dialog.find('.save');
	$cancel = $dialog.find('.cancel');
	$save.click(function(){
		$('#function_action_form').isValid(function(v) {
			if(v){
				var url = 'function/action/save.do';
				var data = $('#function_action_form').serialize()+"&addFlag="+isAdd + "&funSn=${funSn}&systemId=${systemId}";
				var successCallBack = function(result){
					$dialog.dialog('close', dialogId);
					refreshFunctionActionDataGrid();
				}
				var failCallBack = null;
				var errorCallBack = null;
				myAjax(url, data, true, successCallBack, failCallBack, errorCallBack);
			}
		});
	});

	$cancel.click(function(){
		$dialog.dialog('close', dialogId);
	});
}

function refreshFunctionActionDataGrid() {
	functionActionDataGrid.datagrid("refresh");
}
</script>
<div class="bjui-pageContent">
    <div style="padding:0px; height:100%; width:100%;">
        <table id="function-action-datagrid-json" data-width="100%" data-height="100%" class="table table-bordered">
        </table>
    </div>
</div>
<div class="bjui-pageFooter">
	<ul>
		<li><button type="button" class="btn btn-red cancel" data-icon="remove">关闭</button></li>
	</ul>
</div>
<div id="edit-function-action-dialog" style="display: none;">
</div>